﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OpenDNS_Switcher
{
    public partial class frmOpenDNSSwitcher : Form
    {
        public frmOpenDNSSwitcher()
        {
            InitializeComponent();

            LoadCurrentSettings();
        }

        private void LoadCurrentSettings()
        {
            DataSet dsCurrentSettings = new DataSet();
            dsCurrentSettings.ReadXml(GetPathOfConfigurationDatabase());

            string strOpenDNS = dsCurrentSettings.Tables["Settings"].Rows[0]["IsOpenDNS"].ToString();
            string strIsDhcp = dsCurrentSettings.Tables["Settings"].Rows[0]["IsDHCP"].ToString();

            if (strOpenDNS == "TRUE")
                radOpenDNS.Checked = true;
            else if (strIsDhcp == "TRUE")
                radUseDHCP.Checked = true;
            else
                radClassicDNS.Checked = true;

            if (radClassicDNS.Checked)
            {
                string strCustomDnsValue = dsCurrentSettings.Tables["Settings"].Rows[0]["CustomDNSIP"].ToString();

                if (strCustomDnsValue.Length < 1)
                {
                    ArrayList arrCurrentDns = new ArrayList();

                    ArrayList alNicEntries = WMIHelper.GetNICNames();
                    foreach (string strNicName in alNicEntries)
                    {
                        WMIHelper.GetDns(strNicName, ref arrCurrentDns);
                    }

                    dsCurrentSettings.Tables["Settings"].Rows[0]["CustomDNSIP"] = string.Join(",", arrCurrentDns.ToArray());
                }
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private string GetPathOfConfigurationDatabase()
        {
            string strPathOfConfigurationDatabase = string.Format("{0}/data.xml", AppDomain.CurrentDomain.BaseDirectory);

            if (!File.Exists(strPathOfConfigurationDatabase))
            {
                MessageBox.Show("The configuration database does not exist. Application exiting", Constants.APPLICATION_NAME, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                Environment.Exit(-1);
                return (string.Empty);
            }
            else
            {
                return (string.Format("{0}/data.xml", AppDomain.CurrentDomain.BaseDirectory));
            }
        }

        private void btnSaveAndApply_Click(object sender, EventArgs e)
        {
            bool blnDnsSaved = false;

            if (radClassicDNS.Checked)
            {
                ToggleDHCP(false);
                ToggleOpenDNS(false);
                blnDnsSaved = true;
            }
            else if (radOpenDNS.Checked)
            {
                ToggleDHCP(false);
                ToggleOpenDNS(true);
                blnDnsSaved = true;
            }
            else if (radUseDHCP.Checked)
            {
                ArrayList alNicEntries = WMIHelper.GetNICNames();
                foreach (string strNicName in alNicEntries)
                {
                    WMIHelper.SetDHCP(strNicName);
                }
                ToggleDHCP(true);
                blnDnsSaved = true;
            }
            else
            {
                MessageBox.Show("Please select an option to proceed", Constants.APPLICATION_NAME, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (blnDnsSaved)
                MessageBox.Show("Dns Settings Saved", Constants.APPLICATION_NAME,MessageBoxButtons.OK,MessageBoxIcon.Information);
        }

        private void ToggleOpenDNS(bool blnTurnOn)
        {
            DataSet dsCurrentSettings = new DataSet();
            dsCurrentSettings.ReadXml(GetPathOfConfigurationDatabase());
            dsCurrentSettings.Tables["Settings"].Rows[0]["IsOpenDNS"] = blnTurnOn.ToString().ToUpper();

            dsCurrentSettings.AcceptChanges();

            if (blnTurnOn)
            {
                ArrayList alNicNames = WMIHelper.GetNICNames();
                foreach (string s in alNicNames)
                {
                    WMIHelper.SetDns(s, dsCurrentSettings.Tables[0].Rows[0]["OpenDNSIP"].ToString());
                }
            }
            dsCurrentSettings.WriteXml(GetPathOfConfigurationDatabase());
        }

        private void ToggleDHCP(bool blnTurnOn)
        {
            DataSet dsCurrentSettings = new DataSet();
            dsCurrentSettings.ReadXml(GetPathOfConfigurationDatabase());
            dsCurrentSettings.Tables["Settings"].Rows[0]["IsDHCP"] = blnTurnOn.ToString().ToUpper();

            dsCurrentSettings.AcceptChanges();

            if (blnTurnOn)
            {
                ArrayList alNicNames = WMIHelper.GetNICNames();
                foreach (string s in alNicNames)
                {
                    WMIHelper.SetDns(s, string.Empty);
                }
            }

            dsCurrentSettings.WriteXml(GetPathOfConfigurationDatabase());
        }
    }

    public class Constants
    {
        public static readonly string APPLICATION_NAME = Application.ProductName;
    }
}
